﻿#include <stdio.h>
#include <stddef.h>
#include "n2base.h"
#include "FileTable.h"


SrcFileTable::SrcFileTable(size_t  groupCubage) : UniverTab(groupCubage)
{
}

SrcFileTable::SrcFileTable() : UniverTab(64)
{
}

UniverTabItem* SrcFileTable::allocGroupData(size_t count)
{
    return new SrcFile[count];
}

SrcFile* SrcFileTable::getFile(size_t index)
{
    return static_cast<SrcFile*> (getItem(index));
}


LibFileTable::LibFileTable(size_t  groupCubage) : UniverTab(groupCubage)
{
}

LibFileTable::LibFileTable() : UniverTab(32)
{
}

UniverTabItem* LibFileTable::allocGroupData(size_t count)
{
    return new LibFile[count];
}

LibFile* LibFileTable::getFile(size_t index)
{
    return static_cast<LibFile*> (getItem(index));
}

size_t SrcFileBase::read(unsigned char* buffer, size_t readSize)
{
    size_t r;
    if (m_fp == NULL)
        return 0;

    r = fread(buffer, 1, readSize, m_fp);

    if (feof(m_fp)) {
        throw INPUT_FILE_EXCEPTION_EOF;
    }

    return r;
}

size_t SrcFileBase::readInt16(n2u16 * p)
{
    size_t r;
    n2u16 value;
    r = this->read((unsigned char*)(&value), 2);
    if ((m_littleEndian && !little_endian) || (!m_littleEndian && little_endian))
        value = N2Swap16(value);
    *p = value;
    return r;
}

size_t SrcFileBase::readInt32(n2u32 * p)
{
    size_t r;
    n2u32 value;
    r = this->read((unsigned char*)(&value), 4);
    if ((m_littleEndian && !little_endian) || (!m_littleEndian && little_endian))
        value = N2Swap32(value);
    *p = value;
    return r;
}

size_t SrcFileBase::readInt64(n2u64* p)
{
    size_t r;
    n2u64 value;
    r = this->read((unsigned char*)(&value), 8);
    if ((m_littleEndian && !little_endian) || (!m_littleEndian && little_endian))
        value = N2Swap64(value);
    *p = value;
    return r;
}

int SrcFileBase::close()
{
    int r;
    if (m_haveOpen) {
        r = fclose(m_fp);
        m_haveOpen = false;
        m_fp = NULL;
        return r;
    }
    else
        return 0;
}

int SrcFileBase::seek(size_t pos)
{
    long r;
    r = fseek(m_fp, pos, SEEK_SET);
    return (int)r;
}

int SrcFileBase::seekCur(int offset)
{
    return (int)fseek(m_fp, offset, SEEK_CUR);
}

SrcFile::~SrcFile()
{
    close();
    if (m_symbolTab) {
        delete[] m_symbolTab;
        m_symbolTab = NULL;
    }
    if (m_stringTab) {
        delete m_stringTab;
        m_stringTab = NULL;
    }
}

void SrcFile::init(const char * name)
{
    m_fp = NULL;
    m_haveOpen = false;
    m_littleEndian = true;
    m_isInLib = false;
    m_membSize = 0;
    m_membOffset = 0;
    m_filename.setstr(name);
    m_startSectionId = 0;
    m_sectionCount = 0;
    m_symbolTab = NULL;
    m_symbolCnt = 0;
    m_stringTab = NULL;
}

SrcFile::SrcFile()
{
    init("");
}

SrcFile::SrcFile(const char * filename)
{
    init(filename);
}

void SrcFile::moveFrom(UniverTabItem *p)
{
    SrcFile * pSrc;
    pSrc = static_cast<SrcFile*> (p);

    m_fp = pSrc->m_fp;
    m_haveOpen = pSrc->m_haveOpen;
    m_littleEndian = pSrc->m_littleEndian;
    m_isInLib = pSrc->m_isInLib;
    m_membSize = pSrc->m_membSize;
    m_membOffset = pSrc->m_membOffset;
    m_filename = pSrc->m_filename;
    m_startSectionId = pSrc->m_startSectionId;
    m_sectionCount = pSrc->m_sectionCount;
    m_symbolTab = pSrc->m_symbolTab;
    m_symbolCnt = pSrc->m_symbolCnt;
    m_stringTab = pSrc->m_stringTab;

    pSrc->m_haveOpen = false;
    pSrc->m_fp = NULL;
    pSrc->m_symbolTab = NULL;
    pSrc->m_stringTab = NULL;
}

void SrcFile::copyFrom(const UniverTabItem *p)
{

}

UniverTabItem * SrcFile::getOffsetItem(size_t index)
{
    return &this[index];
}

const char * SrcFile::getFileName()
{
    return m_filename.c_str();
}

FILE * SrcFile::openForReadOnly()
{
    FILE * openfp;
    const char * openfilename;
    openfilename = getFileName();

    openfp = fopen(openfilename, "rb");
    if (openfp == NULL) {
        throw INPUT_FILE_EXCEPTION_CANNOT_OPEN;
    }
    m_haveOpen = true;
    m_fp = openfp;
    if (m_isInLib) {
        fseek(openfp, m_membOffset, SEEK_SET);
    }
    return openfp;
}











LibFile::LibFile()
{

}

LibFile::~LibFile()
{

}
void LibFile::moveFrom(UniverTabItem *p)
{

}

void LibFile::copyFrom(const UniverTabItem *p)
{

}

UniverTabItem * LibFile::getOffsetItem(size_t index)
{
    return &this[index];
}



SrcFileTable * input_objs = NULL;                   // 输入的OBJ文件列表
LibFileTable * input_libs = NULL;                   // 输入的LIB文件列表


